package com.gai.shop.dao.impl;

import com.gai.shop.dao.UserDao;
import com.gai.shop.entity.User;
import com.gai.shop.util.DataBaseUtil;
import com.gai.shop.util.StrUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private final QueryRunner QR = new QueryRunner();

    /**
     * 通过用户名查询用户
     * @param uname
     * @return
     * @throws Exception
     */
    @Override
    public User selectUserByUname(String uname) throws Exception {
        return QR.query(DataBaseUtil.getConnection(),
                "SELECT * FROM `user` WHERE uname = ?;",
                new BeanHandler<User>(User.class),
                uname);
    }

    /**
     * 新增用户
     * @param user
     * @return
     * @throws Exception
     */
    @Override
    public Integer addUser(User user) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "INSERT INTO `user` values (DEFAULT ,?,?,?,?,?,?,?)",
                user.getUname(),
                user.getUpassword(),
                user.getUemail(),
                user.getUsex(),
                user.getUstatus(),
                user.getUcode(),
                user.getUrole());
    }

    /**
     * 通过邮箱和激活码进行用户的查询，用于激活
     * @param uemail
     * @param ucode
     * @return
     * @throws Exception
     */
    @Override
    public User selectUserByUemailAndUcode(String uemail, String ucode) throws Exception {
        return QR.query(DataBaseUtil.getConnection(),
                "SELECT * FROM `user` WHERE uemail = ? AND ucode = ?;",
                new BeanHandler<User>(User.class),
                uemail,
                ucode);
    }

    /**
     * 激活用户状态为1
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateUserUstatusByUid(Integer uid) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "UPDATE `user` SET ustatus = 1 WHERE uid = ?;",
                uid);
    }

    /**
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<User> userList() throws Exception {
        return QR.query(DataBaseUtil.getConnection(),
                "SELECT * FROM `user`;",
                new BeanListHandler<>(User.class));
    }

    /**
     * 动态查询
     * @param uname
     * @param usex
     * @return
     * @throws Exception
     */
    @Override
    public List<User> userList(String uname, String usex) throws Exception {
        String sql = "SELECT * FROM user WHERE 1=1 ";
        List<Object> param = new ArrayList<>();
        //1=1
        if (StrUtils.empty(uname)){//uname是有值的
            sql += " AND uname like ?";
            param.add("%" + uname + "%");
        }
        if (StrUtils.empty(usex)){//usex是有值的
            sql += " AND usex = ?";
            param.add(usex);
        }
        //告诉 你这里没有要求采纳数
        return QR.query(DataBaseUtil.getConnection(),
                sql,
                new BeanListHandler<>(User.class),
                param.toArray());
    }

    /**
     * 删除用户
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Integer deleteUser(Integer uid) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "DELETE FROM `user` WHERE uid = ?;",
                uid);
    }
}
